<?

  // convert array hash to url-encoded parameter string
  function array2Url($ar, $concat = '&amp;')
  {
    $result = '';
    if (is_array($ar))
      foreach ($ar as $k => $v)
        if ($k != '' && $v != '')
          $result[] = $k.'='.urlencode($v);
    return(implode($concat, $result));
  }
  
  function interpretQueryString($qs)
  {
    global $config;
    $scriptDirSeg = explode('/', $_SERVER['SCRIPT_NAME']);
    unset($scriptDirSeg[sizeof($scriptDirSeg)-1]);
        
    $scriptDir = implode('/', $scriptDirSeg);
    
    if (strlen($scriptDir)>0)
      $qs = substr($qs, strlen($scriptDir));
      
    $config['site.base'] = 'http://'.$_SERVER['HTTP_HOST'];
    if ($config['site.ignoreport']!= true && $_SERVER['SERVER_PORT'] != 80) 
      $config['site.base'] .= ':'.$_SERVER['SERVER_PORT'];
    $config['site.base'] .= $scriptDir.'/';
    
    if (substr($qs, 0, 1) == '/' && substr($qs, 0, 2) != '/?')
    {
      if (strpos($qs, '?') > 0)
      {
        $sc = CutSegment('?', $qs);
        parse_str($qs, $p);
        foreach($p as $k => $v)
          $_REQUEST[$k] = $v;
        $qs = $sc;      
      }
  
      $seg = explode('/', $qs);
      $config['url_segments'] = $seg;
      $_REQUEST['controller'] = getDefault($seg[1]);
      $_REQUEST['action'] = getDefault($seg[2]);
      unset($seg[0]);
      unset($seg[1]);
      unset($seg[2]);
      $k = null;
      foreach ($seg as $s)
      {
        if ($k == null) 
        {
          $k = $s;  
        }
        else
        {
          $_REQUEST[$k] = $s;
          $k = null;
        }
      }
    }
  }
  
  function selfFormParams($options = array(), $unset = array(), $onlyStandardParams = true)
  {
    global $site;
    if ($onlyStandardParams == true)
    {
      foreach ($_REQUEST as $k => $v)
        if ($k != 'PHPSESSID' && $k != '/~' && $k != 'cmd' && $v != '')
          $optList[$k] = $v;
    }
    else
    {
      $optList['site'] = $_REQUEST['site'];
      $optList['node'] = $_REQUEST['node'];
    }
    foreach ($options as $k => $v)
      $optList[$k] = $v;
    foreach ($unset as $k)
      unset($optList[$k]);
    foreach ($optList as $k => $v)
      print('<input type="hidden" name="'.$k.'" value="'.$v.'"/>');
  }

  function getServerUrl()
  {
    return('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
  }

?>
